<template>
  <div class="register-container">
    <div class="register-content">
      <!-- 头部 -->
      <div class="register-header">
        <el-button 
          icon="ArrowLeft" 
          circle 
          @click="goBack"
          class="back-btn"
        />
        <h1 class="register-title">注册</h1>
      </div>

      <!-- 注册表单 -->
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        @submit.prevent="handleRegister"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            prefix-icon="Lock"
            size="large"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item prop="realName">
          <el-input
            v-model="registerForm.realName"
            placeholder="请输入真实姓名（可选）"
            prefix-icon="UserFilled"
            size="large"
            clearable
          />
        </el-form-item>

        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="请输入邮箱（可选）"
            prefix-icon="Message"
            size="large"
            clearable
          />
        </el-form-item>

        <el-form-item prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="请输入手机号（可选）"
            prefix-icon="Phone"
            size="large"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="register-btn"
            :loading="loading"
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 其他操作 -->
      <div class="register-actions">
        <el-link type="primary" @click="goToLogin">已有账号？立即登录</el-link>
      </div>

      <!-- 用户协议 -->
      <div class="agreement-section">
        <el-checkbox v-model="agreedToTerms">
          我已阅读并同意
          <el-link type="primary" @click="showTerms">《用户协议》</el-link>
          和
          <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
        </el-checkbox>
      </div>
    </div>

    <!-- 用户协议对话框 -->
    <el-dialog
      v-model="termsVisible"
      title="用户协议"
      width="90%"
      :close-on-click-modal="false"
    >
      <div class="terms-content">
        <h3>音乐节助手用户协议</h3>
        <p>欢迎使用音乐节助手应用！</p>
        <p>本协议是您与音乐节助手应用之间的法律协议。</p>
        
        <h4>1. 服务内容</h4>
        <p>我们提供音乐节相关的智能服务，包括但不限于：</p>
        <ul>
          <li>AI智能助手对话</li>
          <li>场地导航和设施查询</li>
          <li>艺人信息和演出管理</li>
          <li>粉丝互动和投票系统</li>
        </ul>

        <h4>2. 用户责任</h4>
        <p>用户在使用本应用时应：</p>
        <ul>
          <li>遵守相关法律法规</li>
          <li>尊重他人权益</li>
          <li>不发布不当内容</li>
          <li>保护个人账户安全</li>
        </ul>
      </div>
      
      <template #footer>
        <el-button @click="termsVisible = false">关闭</el-button>
      </template>
    </el-dialog>

    <!-- 隐私政策对话框 -->
    <el-dialog
      v-model="privacyVisible"
      title="隐私政策"
      width="90%"
      :close-on-click-modal="false"
    >
      <div class="terms-content">
        <h3>音乐节助手隐私政策</h3>
        <p>我们重视您的隐私保护。</p>
        
        <h4>1. 信息收集</h4>
        <p>我们收集的信息包括：</p>
        <ul>
          <li>账户信息（用户名、密码等）</li>
          <li>个人资料（姓名、邮箱、手机号等）</li>
          <li>使用数据（操作记录、偏好设置等）</li>
        </ul>

        <h4>2. 信息使用</h4>
        <p>我们使用收集的信息用于：</p>
        <ul>
          <li>提供和改进服务</li>
          <li>个性化用户体验</li>
          <li>安全防护和风险控制</li>
        </ul>

        <h4>3. 信息保护</h4>
        <p>我们采用行业标准的安全措施保护您的信息。</p>
      </div>
      
      <template #footer>
        <el-button @click="privacyVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { register as registerApi } from '@/api/auth'

// 响应式数据
const loading = ref(false)
const agreedToTerms = ref(false)
const termsVisible = ref(false)
const privacyVisible = ref(false)

// 表单引用
const registerFormRef = ref<FormInstance>()

// 注册表单
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  realName: '',
  email: '',
  phone: ''
})

// 表单验证规则
const registerRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  email: [
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ]
}

// 获取路由
const router = useRouter()

// 返回上一页
const goBack = () => {
  router.back()
}

// 处理注册
const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  if (!agreedToTerms.value) {
    ElMessage.warning('请先同意用户协议和隐私政策')
    return
  }
  
  try {
    await registerFormRef.value.validate()
    
    loading.value = true
    
    const registerData = {
      username: registerForm.username,
      password: registerForm.password,
      realName: registerForm.realName || undefined,
      email: registerForm.email || undefined,
      phone: registerForm.phone || undefined
    }
    
    await registerApi(registerData)
    
    ElMessage.success('注册成功，请登录')
    router.push('/login')
  } catch (error: any) {
    console.error('注册失败:', error)
    ElMessage.error(error.message || '注册失败')
  } finally {
    loading.value = false
  }
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/login')
}

// 显示用户协议
const showTerms = () => {
  termsVisible.value = true
}

// 显示隐私政策
const showPrivacy = () => {
  privacyVisible.value = true
}
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-content {
  background: white;
  border-radius: 20px;
  padding: 40px 30px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.back-btn {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}

.register-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.register-form {
  margin-bottom: 20px;
}

.register-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 25px;
  background: linear-gradient(135deg, #ff6b6b 0%, #4ecdc4 100%);
  border: none;
}

.register-actions {
  text-align: center;
  margin-bottom: 20px;
  font-size: 14px;
}

.agreement-section {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.terms-content {
  max-height: 400px;
  overflow-y: auto;
  padding: 20px;
  
  h3 {
    color: #333;
    margin-bottom: 20px;
  }
  
  h4 {
    color: #666;
    margin: 20px 0 10px 0;
  }
  
  p {
    color: #666;
    line-height: 1.6;
    margin-bottom: 10px;
  }
  
  ul {
    color: #666;
    line-height: 1.6;
    margin-bottom: 15px;
    padding-left: 20px;
    
    li {
      margin-bottom: 5px;
    }
  }
}

// 移动端适配
@media screen and (max-width: 768px) {
  .register-container {
    padding: 15px;
  }
  
  .register-content {
    padding: 30px 20px;
  }
  
  .register-title {
    font-size: 20px;
  }
  
  .register-btn {
    height: 45px;
    font-size: 14px;
  }
}
</style> 